<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Journal extends Controller
{
    public function index()
    {   
        // $id = input('session.user.id');
        $id = Session::get('user.id');
        // var_dump($id);
        // var_dump($_SESSION);
        // 查日志
        $logs = Db::name('log')->where('user_id',$id)->limit('10')->select();
        $this->assign('logdata',$logs);
        // $id = input("get.id");
        

        // 查日志数量
        $lc = Db::name('log')->where('user_id',$id)->count();
        $this->assign('lcdata',$lc);
        // var_dump($logs);



        // 查日记
        $diarys = Db::name('diary')->where('user_id',$id)->limit('10')->select();
        $this->assign('diarydata',$diarys);

        // 查日记数量
        $dc = Db::name('diary')->where('user_id',$id)->count();
        $this->assign('dcdata',$dc);
        // echo $dc;




        return view();
    }

    public function journalinfo()
    {
        $id = input('param.id');
        $this->assign('id',$id);
        $name = input('param.name');
        $this->assign('name',$name);
        // var_dump($name);

        if($name == 'log'){
            // 查询日志
            $logs = Db::name('log')->where('id',$id)->find();
            $this->assign('data',$logs);
            // var_dump($logs);
        }else{
            $diarys = Db::name('diary')->where('id',$id)->find();
            $this->assign('data',$diarys);
            // var_dump($diarys);
        }
        return view();

    }

    public function joudel()
    {
        $id = input('param.id');
        $name = input('param.name');
        // var_dump($id);
        // var_dump($name);
        if($name == 'log'){
            echo "<script>alert('是否删除？？');</script>";
            db('log')->where('id',$id)->delete();
        }else{
            db('diary')->where('id',$id)->delete();
        }
        return $this->redirect('Journal/index');
        // die();
    }

    public function jouedit()
    {
        $id = input('param.id');
        // $this->assign('id',$id);
        $name = input('param.name');
        // $this->assign('name',$name);
        // var_dump($name);

        if($name == 'log'){
            // 查询日志
            $log = Db::name('log')->where('id',$id)->find();
            $this->assign('data',$log);
            // var_dump($logs);
        }else{
            $diary = Db::name('diary')->where('id',$id)->find();
            $this->assign('data',$diary);
            // var_dump($diarys);
        }
        return view();
    }

    public function jouadd()
    {   

        return view();
    }

    public function caozuo()
    {
        $action = input('param.a');
        var_dump($action);

        switch($action){
            //接收数据
            case 'add':
                $post = input('post.');
                // $post = Request::instance()->post();
                var_dump($post);

                $title = input('post.title');
                $content = input('post.content');
                $user_id = Session::get('user.id');

                if(empty($title)){
                    echo "<script>alert('标题不能为空！！')</script>";
                }else if(empty($content)){
                    echo "<script>alert('内容不能为空！！')</script>";
                }else{
                    // Db::name()
                }
        }
    }
}
